package com.example.recyclerviewtest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.Toast;

import com.example.recyclerviewtest.ItemDecoration.DividerGridItemDecoration;
import com.example.recyclerviewtest.ItemDecoration.GridDividerItemDecoration;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private String[] data = {"Apple","Banana", "Orange", "Watermelon", "Pear", "Grape",
            "Pineapple","Strawberry", "Cherry", "Mango","Peach" ,"Shaddock" };


    private ArrayList<Fruit> fruits = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView rv = findViewById(R.id.recycler_view);

        //添加分割线
//        DividerItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
//        itemDecoration.setDrawable(getResources().getDrawable(R.drawable.shape_divider));
//        rv.addItemDecoration(itemDecoration);

        //为GridLayoutManager添加分割线，需要使用自定义的itemDecoration
//        DividerGridItemDecoration gridItemDecoration = new DividerGridItemDecoration(this);
        GridDividerItemDecoration gridItemDecoration = new GridDividerItemDecoration(this);
        rv.addItemDecoration(gridItemDecoration);

        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        rv.setLayoutManager(layoutManager);

        initData();
        FruitAdapter adapter = new FruitAdapter(fruits);
        rv.setAdapter(adapter);
        adapter.setOnRecyclerItemClickListener(new FruitAdapter.OnRecyclerItemClickListener() {
            @Override
            public void onRecyclerItemClick(int position) {
                Toast.makeText(MainActivity.this, fruits.get(position).getName(), Toast.LENGTH_SHORT).show();
            }
        });


    }



    private void initData() {
        for (int i = 0; i < 2; i++) {
            Fruit apple = new Fruit(data[0], R.drawable.apple_pic);
            fruits.add(apple);
            Fruit banana = new Fruit(data[1], R.drawable.banana_pic);
            fruits.add(banana);
            Fruit orange = new Fruit(data[2], R.drawable.orange_pic);
            fruits.add(orange);
            Fruit watermelon = new Fruit(data[3], R.drawable.watermelon_pic);
            fruits.add(watermelon);
            Fruit pear = new Fruit(data[4], R.drawable.pear_pic);
            fruits.add(pear);


            Fruit grape = new Fruit(data[5], R.drawable.grape_pic);
            fruits.add(grape);
            Fruit pineapple = new Fruit(data[6], R.drawable.pineapple_pic);
            fruits.add(pineapple);
            Fruit strawberry = new Fruit(data[7], R.drawable.strawberry_pic);
            fruits.add(strawberry);
            Fruit cherry = new Fruit(data[8], R.drawable.cherry_pic);
            fruits.add(cherry);
            Fruit mango = new Fruit(data[9], R.drawable.mango_pic);
            fruits.add(mango);
            Fruit peach = new Fruit(data[10], R.drawable.peach_pic);
            fruits.add(peach);
            Fruit shaddock = new Fruit(data[11], R.drawable.shaddock_pic );
            fruits.add(shaddock);
        }
    }
}
